package com.zxmzy.scaffold.core.ext

import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.text.method.LinkMovementMethod
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.forEach
import androidx.core.view.marginEnd
import androidx.core.view.marginStart

/**
 * @author zhangxiaoming
 * @date 2023/7/27
 * @description
 */

fun TextView.getViewWidth(): Float {
    return paint.measureText(text.toString()) + paddingStart + paddingEnd + marginStart + marginEnd
}

fun getTextGroupWidth(view: ViewGroup): Float {
    var width = 0f
    view.forEach {
        if (it is TextView) {
            width += it.getViewWidth()
        }
    }
    return width
}

/**
 * 给TextView添加删除线
 */
fun TextView.strikeThroughText() {
    paintFlags = paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
}

/**
 * 给TextView取消删除线
 */
fun TextView.unStrikeThroughText() {
    paintFlags = paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
}

/**
 * 设置字体加小粗
 */
fun TextView.bold() {
    paint.isFakeBoldText = true
}

/**
 * 设置字体加大粗
 */
fun TextView.Bold() {
    typeface = Typeface.defaultFromStyle(Typeface.BOLD)
}

fun TextView.compoundDrawable(
    start: Int = 0,
    top: Int = 0,
    end: Int = 0,
    bottom: Int = 0
) = apply {
    setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom)
}

fun TextView.compoundDrawable(
    start: Drawable? = null,
    top: Drawable? = null,
    end: Drawable? = null,
    bottom: Drawable? = null
) = apply {
    setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom)
}

fun TextView.sizeSpan(
    str: String = "",
    range: IntRange,
    scale: Float = 1.5f
): TextView {
    text = str.ifEmpty { text }.toSizeSpan(range, scale)
    return this
}

fun TextView.appendSizeSpan(str: String = "", scale: Float = 1.5f): TextView {
    append(str.toSizeSpan(0..str.length, scale))
    return this
}

fun TextView.colorSpan(
    str: String = "",
    range: IntRange,
    color: Int = Color.RED
): TextView {
    text = str.ifEmpty { text }.toColorSpan(range, color)
    return this
}

fun TextView.appendColorSpan(
    str: String = "",
    color: Int = Color.RED
): TextView {
    append(str.toColorSpan(0..str.length, color))
    return this
}

fun TextView.backgroundColorSpan(
    str: String = "",
    range: IntRange,
    color: Int = Color.RED
): TextView {
    text = str.ifEmpty { text }.toBackgroundColorSpan(range, color)
    return this
}

fun TextView.appendBackgroundColorSpan(
    str: String = "",
    color: Int = Color.RED
): TextView {
    append(str.toBackgroundColorSpan(0..str.length, color))
    return this
}

fun TextView.strikeThroughSpan(str: String = "", range: IntRange): TextView {
    text = str.ifEmpty { text }.toStrikeThroughSpan(range)
    return this
}

fun TextView.appendStrikeThroughSpan(str: String = ""): TextView {
    append(str.toStrikeThroughSpan(0..str.length))
    return this
}

fun TextView.clickSpan(
    str: String = "",
    range: IntRange,
    color: Int = Color.RED,
    isUnderlineText: Boolean = false,
    clickAction: () -> Unit
): TextView {
    movementMethod = LinkMovementMethod.getInstance()
    highlightColor = Color.TRANSPARENT // remove click bg color
    text = str.ifEmpty { text }.toClickSpan(range, color, isUnderlineText, clickAction)
    return this
}

fun TextView.appendClickSpan(
    str: String = "",
    color: Int = Color.RED,
    isUnderlineText: Boolean = false,
    clickAction: () -> Unit
): TextView {
    movementMethod = LinkMovementMethod.getInstance()
    highlightColor = Color.TRANSPARENT  // remove click bg color
    append(str.toClickSpan(0..str.length, color, isUnderlineText, clickAction))
    return this
}

fun TextView.styleSpan(
    str: String = "",
    range: IntRange,
    style: Int = Typeface.BOLD
): TextView {
    text = str.ifEmpty { text }.toStyleSpan(range, style)
    return this
}

fun TextView.appendStyleSpan(
    str: String = "",
    style: Int = Typeface.BOLD
): TextView {
    append(str.toStyleSpan(0..str.length, style))
    return this
}